﻿package app.view.components.sections.children 
{
	
	import app.model.RemoteProxy;
	import app.model.types.SectionType;
	import app.model.vo.SectionVO;
	import app.view.ApplicationMediator;
	import app.view.components.buttons.GlamurBtnBase;
	import app.view.components.guiDima.VideoItem;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import com.snzflash.components.grid.Grid;
	import flash.events.MouseEvent;
	
	/**
	 * 2.1.3  10 ИНТЕРВЬЮ ПОБЕДИТЕЛЕЙ
	 * @author www
	 */
	public class ChildrenInterviewWinersMBase extends LabeledSectionBase
	{
		//  app.view.components.sections.children.ChildrenInterviewWinersMBase
		public var winersBtn:GlamurBtnBase;
		public var stagesBtn:GlamurBtnBase;
		private var _grid:Grid;
		private var _videoItemArray:Array;
		
		
		public function ChildrenInterviewWinersMBase() 
		{
			super();
		}
		
		override protected function init():void
		{
			super.init();
			
			var col:Number = 5;
			var gh:Number = 30;
			var gv:Number = 30;
			var w:Number = VideoItem.WIDTH*col + (col - 1) * gh+5;
			var h:Number = VideoItem.HEIGHT*col + (col - 1) * gv+5;
			
			//trace('w=', w);
			// настроим таблицу
			_grid = new Grid();
			_grid.width = w;
			_grid.height = h;
			//_grid.numRows = 2;
			_grid.numColumns = 5;
			_grid.horizontalGap = gh;
			_grid.verticalGap = gv;
			_grid.itemRenderer = VideoItem;
			addChild(_grid);
			_grid.x = (ApplicationMediator.SCREEN_WIDTH - w) / 2;
			_grid.y = 280;
			
		}
		
		private function setEvents():void
		{
			stagesBtn.addEventListener(MouseEvent.MOUSE_DOWN, onCLickCompetitionStage);
			winersBtn.addEventListener(MouseEvent.MOUSE_DOWN, onCLickCompetitionWiners);
			
			addEventListener(UIEvent.CLICK_INTERVIEW_WINNER, onClickInterviewWinner, true);
		}
		
		private function removeEvents():void
		{
			stagesBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onCLickCompetitionStage);
			winersBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onCLickCompetitionWiners);
			removeEventListener(UIEvent.CLICK_INTERVIEW_WINNER, onClickInterviewWinner, true);
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			stagesBtn.destroy();
			winersBtn.destroy();
			
			try 
			{
				_grid.destroy();
				removeChild(_grid);
			}catch (err:Error)
			{
				trace('e ');
			}
			
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			setParamHead(HeadMarkerBase.BGD_ORANGE,String(_data.data.datavideo.head[1].text).substr(1));
			
			stagesBtn.setLang(data.lang);
			winersBtn.setLang(data.lang);
			
			
			//trace('data!!!',_data.data.cat[1].image_text[1].image[1].image_path);
			//trace('data!!!',_data.data.cat[1].image_text[1].text[1]);
			
			_videoItemArray = new Array();
			
			
			
			//trace('image',_data.data.video_extends[1].image[1].image_path);
			//trace('flv',_data.data.video_extends[1].flv[1].flv_path);
			//trace('text1',_data.data.video_extends[1].text[1]);
			//trace('text2',_data.data.video_extends[1].text[2]);
			//trace('text3',_data.data.video_extends[1].text[3]);
			
			/*for (var name:String in _data.data.datavideo.video_extends[1].text) 
			{
				//trace('name = ', name);
			}*/
			
			var arr:Array = new Array();
			var ind:int = 0;
			for each(var object:* in _data.data.datavideo.video_extends)
			{
				//trace('ddd', object.flv[1].flv_path);
				var objInfo:Object = new Object();
				objInfo.id 			= ind;
				objInfo.image 		= RemoteProxy.DOMEN + object.image[1].image_path;
				objInfo.flv = (object.flv[1].flv_path != '') ? RemoteProxy.DOMEN + object.flv[1].flv_path : null;
				
				objInfo.text_video 	= object.text[1];
				objInfo.text_image 	= object.text[2];
				objInfo.text_fio 	= object.text[3];
				
				arr.push( objInfo );
				ind++;
			}
			
			// провери чтобы не было больше десяти? nfr crfpfnm juhfybxtybt
			for (var i:int = 0; i < 10; i++) 
			{
				_videoItemArray.push( arr[i] );
			}
			arr = null;
			
			_grid.dataProvider = _videoItemArray as Array;
			addChild(_grid);
			
			setEvents();
		};
		
		/**
		 * переход этапы проведения конкурса
		 * @param	e
		 */
		private function onCLickCompetitionStage(e:MouseEvent):void 
		{
			removeEvents();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_CHILDREN_COMPETITION_STAGES;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
		}
		/**
		 * переход победители
		 * @param	e
		 */
		private function onCLickCompetitionWiners(e:MouseEvent):void 
		{
			removeEvents();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_CHILDREN_COMPETITION_WINERS;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
		}
		
		private function onClickInterviewWinner(e:UIEvent):void 
		{
			//trace('id++=',e.data.id);
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.POPUP_CHILDREN_WINER_INTERVIEW;
			obj.currentId = e.data.id;
			obj.params = data;
			obj.data = _videoItemArray;
			
			var ev:UIEvent = new UIEvent(UIEvent.POPUP_OPEN);
			ev.data = obj;
			dispatchEvent(ev);
		}
		
	}

}